package string;
/**
* String 和  StringBuilder
*/
public class StringBuilderDemo {
    public static void main(String[] args) {
        String str = "好好学习java";
        //复制str的内容到builder中----好好学习Java
        StringBuilder builder = new StringBuilder(str);
        //StringBuffer builder = new StringBuilder(str);
        //StringBuffer 和StringBuilder 用起来大致一样，一般用StringBuilder

        //append():追加内容，在末尾追加
        builder.append(",为了找到一个好工作");
        System.out.println(builder);//好好学习java,为了找到一个好工作

        //replace():替换内容，含头不含尾
        builder.replace(4,8,"嗯啊嗯啊");//替换下标4-7的
        System.out.println(builder);//好好学习嗯啊嗯啊,为了找到一个好工作

        //delete():删除内容，含头不含尾
        builder.delete(4,8);//删除下标4-7的
        System.out.println(builder);//好好学习,为了找到一个好工作

        //insert():插入内容
        builder.insert(0,"人活着就要");//从下标是0的位置插入
        System.out.println(builder);//人活着就要好好学习,为了找到一个好工作


        //String 和  StringBuilder的演示互转
       /*
        //StringBuilder
        StringBuilder builder1 = new StringBuilder();//空串
        StringBuilder builder2 = new StringBuilder("abc");//abc串

        //String和StringBuilder 互转
        String str = "abc";
        StringBuilder builder3 = new StringBuilder(str);//String转StringBuilder
        String str2 = builder3.toString();//StringBuilder转String

        */



        //演示String 和  Stringbuilder的修改速度
/*

        String s ="a";
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            s += i;
        }
        long eddTime = System.currentTimeMillis();
        System.out.println(eddTime-startTime);
        System.out.println("用+号连接的时间");

        StringBuilder builder = new StringBuilder("a");
        long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            builder.append(i);
        }
        long eddTime1 = System.currentTimeMillis();
        System.out.println(eddTime1-startTime1);
        System.out.println("用StringBuilder的时间");
*/

    }
}
